package com.zz.controller;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import com.lowagie.text.DocumentException;
import com.zz.util.Doc2PdfUtil;
import com.zz.util.Img2PdfUtil;
import com.zz.util.Text2PdfUtil;
import org.apache.commons.io.IOUtils;
import org.jodconverter.DocumentConverter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;


@Controller
public class Doc2PdfController {

    @Value("${fileUpLoadPath}")
    private String filePath;

//    @Autowired
//    private DocumentConverter converter;



    private Logger logger = LoggerFactory.getLogger(Doc2PdfController.class);

    @RequestMapping("/hello")
    @ResponseBody
    public String hello(){
        return "hello!";
    }

    @RequestMapping("/getdoc")
    public String getfile(){
        return "getdoc";
    }

    @RequestMapping("/view")
    public String view(){
        return "viewer";
    }

    @RequestMapping("/index")
    public String index(){
        return "index";
    }

    @RequestMapping("/doc2pdf")
    @ResponseBody
    public String doc2pdf(Model model, MultipartHttpServletRequest multiReq) throws IOException {
        MultipartFile multipartFile= multiReq.getFile("file");
        String filename=multipartFile.getOriginalFilename();
        System.out.println("word文件名字："+multipartFile.getOriginalFilename());
        String fileNosu=filename.substring(0,filename.lastIndexOf('.'));
        String destFilename=fileNosu+".pdf";
        String sourceFile=filePath+filename;      /*源word文件路径*/
        String destFile=filePath+destFilename;    /*要生成的pdf路径*/
//        File sourceFile=new File(filePath+filename);

//        multipartFile.transferTo(sourceFile);

        Doc2PdfUtil doc2PdfUtil=new Doc2PdfUtil();
        doc2PdfUtil.createPDF(sourceFile,destFile);

        //使用response,将pdf文件以流的方式发送
//        ServletOutputStream outputStream=response.getOutputStream();
//        InputStream in=new FileInputStream(new File(destFile));
//        int i= IOUtils.copy(in,outputStream);
//        System.out.println(i);
//        in.close();
//        outputStream.close();

        String success="convert success";
        return success;
    }

    @RequestMapping("/img2pdf")
    @ResponseBody
    public String img2pdf(MultipartHttpServletRequest multiReq) throws IOException {
        MultipartFile multipartFile= multiReq.getFile("file");
        String filename=multipartFile.getOriginalFilename();
        System.out.println("img名字："+multipartFile.getOriginalFilename());
        String fileNosu=filename.substring(0,filename.lastIndexOf('.'));
        String destFilename=fileNosu+".pdf";
        String sourceFile=filePath+filename;
        String destFile=filePath+destFilename;
//        File sourceFile=new File(filePath+filename);

//        multipartFile.transferTo(sourceFile);

        Img2PdfUtil img2PdfUtil=new Img2PdfUtil();
        Boolean topdf=img2PdfUtil.imgToPdf(sourceFile,destFile);
        if(topdf==true){
            return "img to pdf success";
        }else {
            return "not found this image";
        }

    }

    @RequestMapping("/txt2pdf")
    @ResponseBody
    public String txt2pdf(MultipartHttpServletRequest multiReq) throws IOException, DocumentException {
        MultipartFile multipartFile= multiReq.getFile("file");
        String filename=multipartFile.getOriginalFilename();
        System.out.println("text文件名字："+multipartFile.getOriginalFilename());
        String fileNosu=filename.substring(0,filename.lastIndexOf('.'));
        String destFilename=fileNosu+".pdf";
        String sourceFile=filePath+filename;
        String destFile=filePath+destFilename;
//        File sourceFile=new File(filePath+filename);

//        multipartFile.transferTo(sourceFile);

        Text2PdfUtil text2PdfUtil=new Text2PdfUtil();
        text2PdfUtil.text2pdf(sourceFile,destFile);

        return "text to pdf success";



    }
}